package com.zulu.watchall.ui.widget;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface.OnShowListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.zulu.watchall.R;

public class CustomAlertDialog {
	private Dialog mCustomDialog;
	private TextView tvTitle;
	private Button mButton1, mButton2;
	private LayoutInflater mInflater;
	private CharSequence[] items;
	private OnItemClickListener itemClickListner;
	private ListView lvContent;
	private Context mContext;
	
	public CustomAlertDialog(Context context, View contentView) {
		mInflater = LayoutInflater.from(context);
        View customLayout = mInflater.inflate(R.layout.custom_dialog_layout, null);
        FrameLayout layoutContent = (FrameLayout)customLayout.findViewById(R.id.layout_content);
        mButton1 = (Button)customLayout.findViewById(R.id.button1);
        mButton2 = (Button)customLayout.findViewById(R.id.button2);
        mButton1.setVisibility(View.GONE);
        mButton2.setVisibility(View.GONE);
        tvTitle = (TextView)customLayout.findViewById(R.id.tv_dialog_title);
        layoutContent.addView(contentView);
        mCustomDialog = new Dialog(context,R.style.CustomDialogTheme);
        mCustomDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        mCustomDialog.setContentView(customLayout);
        mCustomDialog.setCanceledOnTouchOutside(false);
	}
    
	public CustomAlertDialog(Context context, View contentView, int width, int height) {
		mInflater = LayoutInflater.from(context);
        View customLayout = mInflater.inflate(R.layout.custom_dialog_layout, null);
        FrameLayout layoutContent = (FrameLayout)customLayout.findViewById(R.id.layout_content);
        mButton1 = (Button)customLayout.findViewById(R.id.button1);
        mButton2 = (Button)customLayout.findViewById(R.id.button2);
        mButton1.setVisibility(View.GONE);
        mButton2.setVisibility(View.GONE);
        tvTitle = (TextView)customLayout.findViewById(R.id.tv_dialog_title);
        layoutContent.addView(contentView);
//        contentView.setLayoutParams(params)
        mCustomDialog = new Dialog(context,R.style.CustomDialogTheme);
        mCustomDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        mCustomDialog.setContentView(customLayout);
        mCustomDialog.getWindow().setLayout(width, height);
        mCustomDialog.setCanceledOnTouchOutside(false);
	}
	
	public CustomAlertDialog(Context context)
	{
		mContext = context;
		mInflater = LayoutInflater.from(context);
	}
	
//	public void setMultiChoiceItems(CharSequence[] items, boolean[] checkItems){
//		this.items = items;			
//		View customLayout = mInflater.inflate(R.layout.custom_dialog_mutichoice_layout, null);
//        lvContent = (ListView)customLayout.findViewById(R.id.lvContent);
//        mButton1 = (Button)customLayout.findViewById(R.id.button1);
//        mButton2 = (Button)customLayout.findViewById(R.id.button2);
//        mButton1.setVisibility(View.GONE);
//        mButton2.setVisibility(View.GONE);
//        tvTitle = (TextView)customLayout.findViewById(R.id.tv_dialog_title);
//        lvContent.setAdapter(new ListAdapter(mContext, R.layout.custom_dailog_multichoice_content_layout));
//        setMultiItemsCheck(lvContent, checkItems);	        
//        mCustomDialog = new Dialog(mContext,R.style.CustomDialogTheme);
//        mCustomDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
//        mCustomDialog.setContentView(customLayout);	
//        mCustomDialog.setCanceledOnTouchOutside(false);
//	}
	
//	public void setSingleChoiceItems(CharSequence[] items, int checkItem) {
//		this.items = items;			
//		View customLayout = mInflater.inflate(R.layout.custom_dialog_singlechoice_layout, null);
//        lvContent = (ListView)customLayout.findViewById(R.id.lvContent);
//        mButton1 = (Button)customLayout.findViewById(R.id.button1);
//        mButton2 = (Button)customLayout.findViewById(R.id.button2);
//        mButton1.setVisibility(View.GONE);
//        mButton2.setVisibility(View.GONE);
//        tvTitle = (TextView)customLayout.findViewById(R.id.tv_dialog_title);
//        lvContent.setAdapter(new ListAdapter(mContext, R.layout.custom_dialog_singlechoice_content_layout));
//        setSingleItemCheck(lvContent, checkItem);	        
//        mCustomDialog = new Dialog(mContext,R.style.CustomDialogTheme);
//        mCustomDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
//        mCustomDialog.setContentView(customLayout);
//        mCustomDialog.setCanceledOnTouchOutside(false);
//	}
	
	public void setMessage(String mess) {
		View customLayout = mInflater.inflate(R.layout.custom_dialog_standard_layout, null);
        TextView tvMessage = (TextView)customLayout.findViewById(R.id.content_message);
        mButton1 = (Button)customLayout.findViewById(R.id.button1);
        mButton2 = (Button)customLayout.findViewById(R.id.button2);
        mButton2.setVisibility(View.GONE);
        tvTitle = (TextView)customLayout.findViewById(R.id.tv_dialog_title);
        tvMessage.setText(mess);	        
        mCustomDialog = new Dialog(mContext,R.style.CustomDialogTheme);
        mCustomDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        mCustomDialog.setContentView(customLayout);
        mCustomDialog.setCanceledOnTouchOutside(false);
        mButton1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				mCustomDialog.dismiss();
			}
		});
	}
	
//	public void setConfirmMessage(String mess) {
//		View customLayout = mInflater.inflate(R.layout.custom_dialog_standard_layout, null);
//        TextView tvMessage = (TextView)customLayout.findViewById(R.id.content_message);
//        mButton1 = (Button)customLayout.findViewById(R.id.button1);
//        mButton2 = (Button)customLayout.findViewById(R.id.button2);
//        mButton1.setVisibility(View.GONE);
//        mButton2.setVisibility(View.GONE);
//        tvTitle = (TextView)customLayout.findViewById(R.id.tv_dialog_title);
//        tvMessage.setText(mess);	        
//        mCustomDialog = new Dialog(mContext,R.style.CustomDialogTheme);
//        mCustomDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
//        mCustomDialog.setContentView(customLayout);
//        mCustomDialog.setCanceledOnTouchOutside(false);
//	}
	
	public void setOnItemClickListener(OnItemClickListener itemClick)
	{
		itemClickListner = itemClick;
		lvContent.setOnItemClickListener(itemClickListner);
	}
	
	public void setOnShowDialogListener(OnShowListener listener){
		if(mCustomDialog != null)
			mCustomDialog.setOnShowListener(listener);
	}
	public ListView getListView()
	{
		return lvContent;
	}
	
	private void setMultiItemsCheck(ListView listView, boolean[] checkItems)
	{
		for(int i=0; i < checkItems.length; i++)
		{
			listView.setItemChecked(i, checkItems[i]);
		}
	}
	
	private void setSingleItemCheck(ListView listView, int checkItem)
	{	
			listView.setItemChecked(checkItem, true);
	}
	
	public void showDialog()
	{
		if(mCustomDialog != null && !mCustomDialog.isShowing()){
			mCustomDialog.show();
		}
	}
	
	public void hideDialog()
	{
		if(mCustomDialog != null && mCustomDialog.isShowing()){
			mCustomDialog.dismiss();
		}
	}
	
	public void setTitle(String title)
	{
		if(tvTitle != null)
			tvTitle.setText(title);
	}
	
	public void setButton(String title, View.OnClickListener onClick ){
		mButton1.setVisibility(View.VISIBLE);
		mButton1.setText(title);
		mButton1.setOnClickListener(onClick);
	}
	
	public void setButton2(String title, View.OnClickListener onClick){
		mButton2.setVisibility(View.VISIBLE);
		mButton2.setText(title);
		mButton2.setOnClickListener(onClick);
	}
	
//	public class ListAdapter extends ArrayAdapter<CharSequence> {
//
//	    private LayoutInflater inflater;
//	    private int layoutId;
//	    public ListAdapter(Context context, int layoutId) {
//	        super(context, layoutId);
//	        this.layoutId = layoutId; 
//	        this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//	    }
//
//	    @Override
//	    public View getView(int position, View convertView, ViewGroup parent) {
//	        View view = convertView;
//	        ViewWrapper wrapper = null;
//	        if (view == null) {
//	            view = this.inflater.inflate(layoutId, parent, false);
//	            wrapper = new ViewWrapper();
//	            wrapper.tvName = (TextView)view.findViewById(R.id.tvName);
//	            view.setTag(wrapper);
//	        } else {
//	            wrapper = (ViewWrapper) view.getTag();
//	        }
//
//	        CharSequence name = items[position];
//	        wrapper.tvName.setText(name);
//	    
//	        return view;
//	    }
//
//	    @Override
//	    public int getCount() {
//	        return items.length;
//	    }
//
//	    @Override
//	    public CharSequence getItem(int position) {
//	        return items[position];
//	    }
//
//	    @Override
//	    public long getItemId(int position) {
//	        return position;
//	    }
//
//	    public class ViewWrapper {
//	        public TextView tvName;
//	    }
//	}
	
	public boolean isShowDialog() {
		if(mCustomDialog != null) {
			return mCustomDialog.isShowing();
		}
		return false;
	}
}
